<?php 
class Vts_SearchBox{

    private $_options;
    /**
     * @var array
     */
    private $_data;
    
    /**
     * @var array
     */
    private $_instance;
    
    private $_name;
    
    /**
     * @var Zend_View $_view
     */
    private $_view;
    
    public function __construct($options = array()){
        $this->_options = $options;
    }
    
    public function setName($name){
        $this->_name = $name;
    }
    
    public function setInstance($instance){
        $this->_instance = $instance;
    }
    
    /**
     * Set data
     * @author tien.nguyen
     * @param array $data
     */
    public function setData($data){
        $this->_data = $data;
    }
    
    /**
     * Render form
     *
     * @param  Zend_View_Interface $view
     * @return string
     */
    public function render(Zend_View_Interface $view = null)
    {
        $view = $this->getView();
        
        $view->addBasePath(APPLICATION_PATH.'/../library/Vts/SearchBox');
        $view->data = $this->_data;
        $view->instance = $this->_instance;
        $view->name = $this->_name;
        
        $content = $view->render('search-box.phtml');
        
        
        return $content;
    }
    
    /**
     * Retrieve view object
     *
     * If none registered, attempts to pull from ViewRenderer.
     *
     * @return Zend_View_Interface|null
     */
    public function getView()
    {
        if (null === $this->_view) {
            require_once 'Zend/Controller/Action/HelperBroker.php';
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            $this->setView($viewRenderer->view);
        }

        return $this->_view;
    }
    
    /**
     * Set view
     */
    public function setView($view){
        $this->_view = $view;
    }
    
    /**
     * Serialize as string
     *
     * Proxies to {@link render()}.
     *
     * @return string
     */
    public function __toString()
    {        
        try {
            $return = $this->render();
            return $return;
        } catch (Exception $e) {
            $message = "Exception caught by form: " . $e->getMessage()
                     . "\nStack Trace:\n" . $e->getTraceAsString();
            trigger_error($message, E_USER_WARNING);
            return '';
        }
    }
}